<?php
class Admin_UsersController extends Zend_Controller_Action
{	
    public function init() {
        if (Zend_Auth::getInstance ( 'admin' )->setStorage ( new Zend_Auth_Storage_Session ( 'admin' ) )->hasIdentity () == false) {
			$this->_helper->getHelper ( 'Redirector' )->gotoUrl ( "admin/login" );
		} 
    }
    public function indexAction()
    {
      $this->view->title = "Users";	
    	try {
           $user = new Vina_Model_Users();
           $dataList = $user->fetchListToArray('1=1', 'id desc');
           $this->view->data = $dataList;     
                
         }catch(Exception $ex){
            echo "<pre>";
            print_r($ex->getMessage());
        }
    
    }
    public function addAction() {
        try {
           $user = new Vina_Model_Users();
           $form                   = new Admin_Form_User();
           if ($this->_request->isPost()) {
                $formData = $this->_request->getPost();
    
                if ($form->isValid($formData)) {
                   $user->setUsername($formData['username']);
                   $user->setName($formData['name']);
                   $user->setAddress($formData['address']);
                   $user->setBirthYear($formData['birth_year']);
                   $user->setPassword(md5($formData['password']));
                   $user->save();
                   $this->_redirect('/admin/users');
                }
            }
           //set view
           $this->view->form       = $form;
        }catch(Exception $ex){
            echo "<pre>";
            print_r($ex->getMessage());
        }
    }
    public function editAction() {
        try {
           $user = new Vina_Model_Users();
           $form                   = new Admin_Form_User();
           $form->getElement('username')->setAttrib('readonly','true')->removeValidator('Db_NoRecordExists');
           $form->getElement('password')->removeValidator('NotEmpty')->setRequired(false);
           $id                     =  $this->_request->getParam('id',false);
           $data = $user->find($id)->toArray();
           $form->populate($data);
           if ($this->_request->isPost()) {
                $formData = $this->_request->getPost();
    
                if ($form->isValid($formData)) {
                   $user->setUsername($formData['username']);
                   $user->setName($formData['name']);
                   $user->setAddress($formData['address']);
                   $user->setBirthYear($formData['birth_year']);
                   $user->setPassword(md5($formData['password']));
                   $user->save();
                   $this->_redirect('/admin/users');
                }
            }
           //set view
           $this->view->form       = $form;
        }catch(Exception $ex){
            echo "<pre>";
            print_r($ex->getMessage());
        }
    }
	public function deleteAction() {
     $id                     =  $this->_request->getParam('id',false);
     $users = new Vina_Model_Users();
     $where = "id = $id";
     $users->delete($where);
     $this->_redirect($_SERVER['HTTP_REFERER']);
  }
    	
}
